package net.chrisrichardson.ormunit.hibernate;

import java.util.Collections;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.PersistentClass;

public class HibernateMappingChecker {

	private final Configuration configuration;

	private AccessStrategy accessStrategy = AccessStrategy.PROPERTY;

	public HibernateMappingChecker(Configuration configuration) {
		this.configuration = configuration;
	}

	public void setAccessStrategy(AccessStrategy accessStrategy) {
		this.accessStrategy = accessStrategy;
	}

	public MappedClassChecker getClassChecker(Class type) {
		return new MappedClassChecker(type, configuration, accessStrategy);
	}

	/**
	 * Verify that all classes and their fields are mapped
	 */

	public void assertAllClassesMapped() {
		assertAllClassesMapped(Collections.EMPTY_SET);
	}

	public void assertAllClassesMapped(Set<String> propertyNames) {
		for (Iterator it = configuration.getClassMappings(); it.hasNext();) {
			PersistentClass persistentClass = (PersistentClass) it.next();
			Class mappedClass = (persistentClass).getMappedClass();
			MappedClassChecker classChecker = getClassChecker(mappedClass);
			classChecker.assertAllPropertiesMappedExcept(PathUtil.getPathsStartingWith(mappedClass.getName(), propertyNames));
		}
	}


}
